<?php

namespace app\controller\api\controller;

use app\support\ResponseHelper;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;
use Shayvmo\WebmanAnnotations\Annotations\PutMapping;
use Shayvmo\WebmanAnnotations\Annotations\RestController;
use support\Log;
use support\Response;
use support\Redis;

/**
 * 店铺相关接口
 */
#[RestController("/api/shop")]
class ShopController
{
    /**
     * 店铺状态在Redis中的键名
     */
    public const SHOP_STATUS_KEY = "SHOP_STATUS";

    /**
     * 设置店铺的营业状态
     * @param int $status 营业状态：1-营业中，0-打烊中
     * @return Response
     */
    #[PutMapping("/{status}")]
    public function setStatus(int $status): Response
    {
        try {
            $statusText = $status === 1 ? '营业中' : '打烊中';
            Log::info("设置店铺的营业状态为：{$statusText}");
            
            // 设置店铺状态到Redis
            Redis::set(self::SHOP_STATUS_KEY, $status);
            
            return ResponseHelper::successJson("", "设置店铺状态成功");
        } catch (\Exception $e) {
            Log::error("设置店铺状态失败：" . $e->getMessage());
            return ResponseHelper::failJson("设置店铺状态失败");
        }
    }

    /**
     * 获取店铺的营业状态
     * @return Response
     */
    #[GetMapping("/status")]
    public function getStatus(): Response
    {
        try {
            // 从Redis获取店铺状态
            $status = Redis::get(self::SHOP_STATUS_KEY);
            
            // 如果Redis中没有设置状态，默认为打烊中
            if ($status === null) {
                $status = 0;
            } else {
                $status = (int) $status;
            }
            
            $statusText = $status === 1 ? '营业中' : '打烊中';
            Log::info("获取到店铺的营业状态为：{$statusText}");
            
            return ResponseHelper::successJson($status, "获取店铺状态成功");
        } catch (\Exception $e) {
            Log::error("获取店铺状态失败：" . $e->getMessage());
            return ResponseHelper::failJson("获取店铺状态失败");
        }
    }
}